package com.toilet.easyvideofx;

import atlantafx.base.theme.PrimerLight;
import com.toilet.easyvideofx.api.GreetService;
import com.toilet.easyvideofx.config.HelidonConfig;
import com.toilet.easyvideofx.handler.InitializeHandler;
import com.toilet.easyvideofx.theme.Resources;
import io.helidon.config.Config;
import io.helidon.config.ConfigSources;
import io.helidon.config.spi.ConfigSource;
import io.helidon.config.yaml.YamlConfigParser;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.accesslog.AccessLogFeature;
import io.helidon.webserver.http.HttpRouting;
import io.helidon.webserver.staticcontent.StaticContentService;
import javafx.application.Application;
import javafx.stage.Stage;

import java.io.*;
import java.nio.file.Paths;

/**
 * app主程序
 */
public class ClientApplication extends Application {
    public static final boolean IS_DEV_MODE = "DEV".equalsIgnoreCase(
            Resources.getPropertyOrEnv("atlantafx.mode", "ATLANTAFX_MODE")
    );

    private WebServer server;

    @Override
    public void init() throws Exception {
        super.init();

        // 创建一个YAML配置源
        Config config = Config.create(ConfigSources.classpath("application.yaml"));
        Config.global(config);
        //异步启动服务
        Thread.ofVirtual().start(() -> {
            server = WebServer.builder()
                    .addFeature(AccessLogFeature.builder().commonLogFormat().build())
                    .config(config.get("server"))
                    //路由
                    .routing(HelidonConfig::routing)
                    .build()
                    .start();
            System.out.println("WEB server is up! http://localhost:" + server.port() + "/greet");
            System.out.println("WEB server is up! http://localhost:" + server.port() + "/static/h5Upload.html");
        });
    }


    @Override
    public void start(Stage stage) throws IOException {

        Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
        // 初始化初始化处理器
        InitializeHandler initializeHandler = new InitializeHandler();
        // 开始执行初始化
        initializeHandler.init(stage);
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        server.stop();
    }

    public static void main(String[] args) {
        launch();
    }

    /**
     * 主页面显示
     *
     * @param stage 主页面
     */
    public static void shouMainStage(Stage stage) {
        stage.show();
    }
}